package com.soikc.kuaidiE.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.soikc.kuaidiE.bean.KuaiDi;
import com.soikc.kuaidiE.bean.Message;
import com.soikc.kuaidiE.mvc.ResponseBody;
import com.soikc.kuaidiE.service.KuaiDiService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class KuaiDiWeb {
    private static final KuaiDiService sev = new KuaiDiService();

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse res) {
        JSONObject jsonObject = sev.queryConsole();
        return jsonObject.toJSONString();
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse res) {
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        List<KuaiDi> list = sev.queryList(Integer.parseInt(offset), Integer.parseInt(pageNumber));
        JSONObject jsonObject = sev.queryConsole();
        Integer integer = jsonObject.getJSONObject("data").getInteger("data1_size");
        JSONArray arr = new JSONArray();
        list.forEach(d -> {
            arr.add(d.toViveObj());
        });
        JSONObject data = new JSONObject();
        data.put("rows", arr);
        data.put("total", integer);
        return data.toJSONString();
    }
    @ResponseBody("/express/insert.do")
    public String add(HttpServletRequest req, HttpServletResponse res) {
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        KuaiDi k = new KuaiDi();
        k.setSingle_number(number);
        k.setCompany(company);
        k.setUser_name(username);
        k.setPhone(userPhone);
        boolean add = sev.add(k);
        Message message = new Message(add ? 0 : -1,add?"入录成功":"录入失败");
        return JSON.toJSONString(message);
    }
    @ResponseBody("/express/find.do")
    public String queryI (HttpServletRequest req, HttpServletResponse res){
        String number = req.getParameter("number");
        KuaiDi kuaiDi = sev.querySingle_number(number);
        Message message;
        if (kuaiDi == null){
            message = new Message(-1,"查询不到快递");
        }else {
            message = new Message(0,"查询成功",kuaiDi.toViveObj());
        }
        return message.toString();
    }
    @ResponseBody("/express/update.do")
    public String upDate(HttpServletRequest req, HttpServletResponse res){
        String id = req.getParameter("id");
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String status = req.getParameter("status");
        KuaiDi kuaiDi = new KuaiDi();
        kuaiDi.setId(Integer.parseInt(id));
        kuaiDi.setSingle_number(number);
        kuaiDi.setCompany(company);
        kuaiDi.setUser_name(username);
        kuaiDi.setStatus(Integer.parseInt(status));
        boolean b = sev.update(Integer.parseInt(id), kuaiDi);
        if (b){
            return new Message(0,"修改成功").toString();
        }else {
            return new Message(-1,"修改失败").toString();
        }
    }
    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse res){
        String id = req.getParameter("id");
        boolean b = sev.delID(Integer.parseInt(id));
        Message message;
        if (b){
            message = new Message(0,"删除成功");
        }else {
            message = new Message(-1,"删除失败");
        }
        return message.toString();
    }
}
